<template>
  <ws-table-el ref="table" v-bind="tableConfig" @selection-change="handleSelectionChange">
    <template #search>
      <search-group>
        <label for>问题名称：</label>
        <el-input v-model.trim="searchOptions.appType" placeholder="请输入问题名称" clearable></el-input>
      </search-group>
      <search-group>
        <label for>创建时间：</label>
        <ws-date-range
          :startTime.sync="searchOptions.createTimeStart"
          :picker-options="pickerOptions"
          :endTime.sync="searchOptions.createTimeEnd"
        ></ws-date-range>
      </search-group>
    </template>
    <template #options>
      <el-dropdown @command="handleCommand">
        <el-button type="primary">
          批量操作
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="enable">批量启用</el-dropdown-item>
          <el-dropdown-item command="disable">批量停用</el-dropdown-item>
          <el-dropdown-item command="batchReplace" :class="{ 'disable-dropdown': selectColIds.length }">
            批量导入
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </template>
    <template #searchBtns>
      <el-button-group>
        <ws-enter-event>
          <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
        </ws-enter-event>
        <ws-throttle>
          <el-button
            type="primary"
            class="table-column-header-button"
            @click="reset"
            icon="el-icon-refresh-left"
          ></el-button>
        </ws-throttle>
      </el-button-group>
      <el-button class="add" @click="jumpToForm({ type: 'add' })" plain>新增</el-button>
    </template>
    <template>
      <ws-table-column-el prop="createUser" label="问题ID"></ws-table-column-el>
      <ws-table-column-el prop="appType" label="问题名称"></ws-table-column-el>
      <ws-table-column-el prop="appType1" label="业务类型">
        <template slot-scope="scope">
          {{ scope.row.appType === 1 ? '小程序' : scope.row.appType === 0 ? 'app' : '-' }}
        </template>
      </ws-table-column-el>
      <ws-table-column-el prop="createUser1" label="点击量"></ws-table-column-el>
      <ws-table-column-el prop="createUser2" label="已解决数量"></ws-table-column-el>
      <ws-table-column-el prop="createUser3" label="未解决数量"></ws-table-column-el>
      <ws-table-column-el prop="status" label="状态" width="120px">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'success' : 'info'" size="medium">
            {{ scope.row.status === 1 ? '启用' : '停用' }}
          </el-tag>
        </template>
      </ws-table-column-el>
      <ws-table-column-el prop="createUser4" label="创建人"></ws-table-column-el>
      <ws-table-column-el prop="createTime" label="创建时间"></ws-table-column-el>
      <ws-table-column-el prop="" label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button @click="jumpToForm({ type: 'detail', id: scope.row.id })" type="text" style="color: #0088ff">
            详情
          </el-button>
          <el-button @click="jumpToForm({ type: 'edit', id: scope.row.id })" type="text" style="color: #0088ff">
            修改
          </el-button>
          <el-button
            v-if="scope.row.status === 2"
            type="text"
            @click="updateStatus(scope.row, 'enable')"
            style="color: #0088ff"
          >
            启用
          </el-button>
          <el-button v-if="scope.row.status === 1" type="text" @click="updateStatus(scope.row, 'disable')">
            停用
          </el-button>
        </template>
      </ws-table-column-el>
    </template>
  </ws-table-el>
</template>
<script>
export default {
  name: 'questionManage',
  data() {
    return {
      moduleName: '/customerServiceSystem/questionManage',
      searchOptions: {
        appType: null,
        status: ''
      },
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.minDate = minDate
          this.maxDate = maxDate
        }
      },
      selectColIds: [],
      tableConfig: {
        emptyModel: true,
        indexColumn: true,
        loading: true,
        multi: true,
        data: [
          {
            createUser: '1212',
            appType: '1',
            appType1: '1',
            createUser1: '34',
            createUser2: '2',
            createUser3: '3',
            status: 2,
            createUser4: '张三',
            createTime: '2022-12-21'
          }
        ],
        lazyQuery: {
          // moduleName: '/customerServiceSystem/questionManage',
          op: {
            selectList: 'list',
            selectCount: 'count'
          },
          beforeQuery: this.beforeQuery
        }
      },
      op: {
        updateStatus: 'updateStatus'
      }
    }
  },
  mounted() {},
  methods: {
    search() {
      this.$refs.table.search()
    },
    handleSelectionChange(list) {
      this.selectColIds = list.map((item) => item.id)
    },
    jumpToForm(param) {
      this.$router.push({ name: 'questionManageCommonForm', query: param })
    },
    handleCommand(e) {
      if (e == 'enable' || e == 'disable') {
        if (this.selectColIds.length == 0) {
          this.$message.error('请选择数据')
          return
        }
        this.batchHandle(e)
      } else if (e == 'batchReplace') {
        this.$router.push({ name: 'templateManagebatchAdd' })
      }
    },
    batchHandle(operatorType) {
      this.$axios(this.powerList['switch'], {
        data: { idList: this.selectColIds, operatorType },
        op: 'switch'
      })
        .then(() => {
          this.$message.success(`状态修改成功`)
          this.search()
        })
        .catch((err) => {
          console.log('err', err)
        })
    },
    beforeQuery() {},
    updateStatus(dataRow, operatorType) {
      let data = {
        idList: [dataRow.id],
        operatorType
      }
      let msg = operatorType == 'enable' ? '启用' : '停用'
      this.$confirm(`确定要${msg}吗`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$axios(this.powerList['switch'], {
          data,
          op: 'switch'
        })
          .then(() => {
            this.$message.success(`${msg}成功`)
            this.search()
          })
          .catch((err) => {
            console.log('err', err)
          })
      })
    },
    reset() {
      this.$refs.table.reset()
    }
  }
}
</script>
<style lang="scss" scoped>
.add {
  margin: 0px 20px;
}
.disable-dropdown {
  cursor: not-allowed !important;
  pointer-events: auto;
}
</style>
